package com.SpringBoot.security;

import com.SpringBoot.user.pojo.User;
import com.SpringBoot.rolePermission.mapper.RolePermissionMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import com.SpringBoot.user.mapper.UserMapper;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 自定义UserDetailsService
 * 将其注入到容器中 替换原始的InMemoryUserDetailsManager
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据账号 查询用户对象
        User user = userMapper.selectByUsername(username);
        //根据用户id 查询用户角色列表
        ArrayList<String> menuList = rolePermissionMapper.selectUserPermissionByUserId(user.getUserId());
        MyUserDetails userDetails = new MyUserDetails();
        userDetails.setUser(user);
        userDetails.setMenuList(menuList);
        return userDetails;
    }
}
